import re

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

import time
import random
import urllib

from getPosition import get_pos

# 谷歌driver 110版本
# 创建 Chrome WebDriver 选项
# chrome_options = Options()
# # 使用代理ip（自己本机ip被封禁了）
# proxy_ip = "113.142.94.234"  # 换成自己的代理ip
# proxy_port = "5412"  # 换成自己的代理端口号
# chrome_options.add_argument('--proxy-server={}:{}'.format(proxy_ip, proxy_port))
# 手机号登陆
driver = webdriver.Chrome()
try:
    driver.get('https://accounts.douban.com/passport/login?source=movie')
    time.sleep(5)
    print('**********正在点击短信登陆模块')
    mobile_click = driver.find_element(By.XPATH,'//*[@id="account"]/div[2]/div[2]/div/div[1]/ul[1]/li[1]')
    mobile_click.click()
    time.sleep(3)
    print('**********正在填写手机号')
    mobileInput = driver.find_element(By.NAME,'phone')
    mobileInput.send_keys('19959797519')
    time.sleep(3)
    print('**********正在点击获取验证码')
    getcodeClick = driver.find_element(By.CLASS_NAME,'get-code')
    getcodeClick.click()
    print('**********弹出滑块框，进行滑块处理')
    # 浏览器隐式等待
    driver.implicitly_wait(5)
    # 此时需要切换到弹出的滑块区域，需要切换frame窗口
    driver.switch_to.frame('tcaptcha_iframe_dy')
    # 等待滑块验证码图片加载后，再做后面的操作
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'slideBg')))
    # 获取滑块验证图片下载路径，并下载到本地
    bigImage = driver.find_element(By.ID, 'slideBg')
    s = bigImage.get_attribute("style")
    # 获取图片的下载地址
    p = 'background-image: url\(\"(.*?)\"\);'
    bigImageSrc = re.findall(p, s, re.S)[0]
    print(bigImageSrc)
    urllib.request.urlretrieve(bigImageSrc, 'bigImage.png')
    dis = get_pos(imageSrc='bigImage.png')
    time.sleep(5)

    # 定位滑块元素
    slide = driver.find_element(By.XPATH, '//*[@id="tcOperation"]/div[6]')

    # 小滑块到目标区域的移动距离（缺口坐标的水平位置距离小滑块的水平坐标相减的差）
    # 新缺口坐标=原缺口坐标*新画布宽度/原画布宽度
    newDis = int(dis * 340 / 672 - slide.location['x'])
    driver.implicitly_wait(5)

    # 按下小滑块按钮不动
    ActionChains(driver).click_and_hold(slide).perform()
    # 移动小滑块，模拟人的操作，一次次移动一点点
    i = 0
    moved = 0
    while moved < newDis:
        x = random.randint(3, 10)
        moved += x
        ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
        print("第{}次移动后，位置为{}".format(i, slide.location['x']))
        i += 1
    # 移动完之后，松开鼠标
    ActionChains(driver).release().perform()
    time.sleep(5)
    print('**********获取验证码中')
    code = input('请输入你获得的验证码')
    # 退出刚刚切换的iframe（一定要做）我自己在这卡了好久
    driver.switch_to.default_content()
    print('**********正在填写验证码中')
    codeInput = driver.find_element(By.XPATH,'//*[@id="code"]').send_keys(code)
    time.sleep(4)
    print('**********正在点击登陆')
    LoginClick = driver.find_element(By.XPATH,'//*[@id="account"]/div[2]/div[2]/div/div[2]/div[1]/div[5]/a').click()
    time.sleep(5)
    print('登陆成功->豆瓣')

finally:
    driver.quit()